//
//  WCNMulTextEditVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/1.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNMulTextEditVC.h"
//
#import "WCButton.h"
//
#import "UINavigationItem+Offset.h"
#import "NSString+Utilities.h"
//
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"

@interface WCNMulTextEditVC () <UITextViewDelegate>

@property (nonatomic) WCNMulTextEdit type;
@property (weak, nonatomic) IBOutlet UILabel *placeholder;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic, strong) WCButton *rightBtn;

@end

@implementation WCNMulTextEditVC

- (instancetype)initWithType:(WCNMulTextEdit)type
{
    self = [super init];
    if (self) {
        _type = type;
        _canEmpty = NO;
        self.unAbleEdit = NO;
        _textViewEmptyWarm = @"请输入内容";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (self.textViewPlaceholder) {
        self.placeholder.numberOfLines = 0;
        self.placeholder.text = self.textViewPlaceholder;
    }
    if (self.textViewValue) {
        self.textView.text = self.textViewValue;
        self.textView.backgroundColor = [UIColor whiteColor];
    }

    if (self.unAbleEdit == NO) {
        WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeSave];
        [rightBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
        self.rightBtn = rightBtn;
        [self.navigationItem addRightBarButtonItem:rightBtn];
        
        
    }else{
        self.textView.editable = NO;
        
    }
}

- (void)save
{
    NSString *content = self.textView.text;
    if (!self.canEmpty && [content isEmpty]) {
        [[WCPopupLayerManager shareMg] showTextOnly:self.textViewEmptyWarm withView:self.view];
        return;
    }
    
    if (self.type == WCNMulTextEditSave) {
        self.callback(content);
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        //访问请求
        [self.params setValue:content forKey:self.paramKey];
        
        MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
        [[WCNetworkAccessManager sharedMg] POST:self.url parameters:self.params success:^(NSDictionary *result){
            [[WCPopupLayerManager shareMg] showTextOnly:result[@"errMsg"] withView:self.view];
            
            [self performSelector:@selector(saveSuccess) withObject:nil afterDelay:1.0];
        } failure:nil netError:nil always:^{
            [hud hide:YES];
        }];
    }
}
 -(void)setUnAbleEdit:(BOOL)unAbleEdit
{
    _unAbleEdit = unAbleEdit;

}
- (void)saveSuccess
{
    if (self.callback) {
        self.callback(nil);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Nib Actions

- (IBAction)bgTap:(id)sender {
    [self.view endEditing:YES];
}

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *txt = textView.text;
    if (txt.length == 0) {
        textView.backgroundColor = [UIColor clearColor];
    } else {
        textView.backgroundColor = [UIColor whiteColor];
    }
}

@end
